package com.xiaoye.qbproject.controller.qbzt;

import com.xiaoye.qbproject.controller.base.BaseController;
import com.xiaoye.qbproject.domain.AjaxResult;
import com.xiaoye.qbproject.domain.QbSubjectArticle;
import com.xiaoye.qbproject.domain.wiki.QbPublishVo;
import com.xiaoye.qbproject.page.TableDataInfo;
import com.xiaoye.qbproject.service.MqReadService;
import com.xiaoye.qbproject.service.QbSubjectArticleService;
import com.xiaoye.qbproject.vo.QbSubjectArticleVo;
import com.xiaoye.qbproject.vo.ReadMsgVo;
import lombok.extern.slf4j.Slf4j;
import org.codehaus.plexus.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Slf4j
@RestController
@RequestMapping("/article")
public class QbSubjectArticleController extends BaseController {
    @Autowired
    private QbSubjectArticleService qbSubjectArticleService;

    // 文章列表
    @GetMapping("/list")
    public TableDataInfo list(QbSubjectArticleVo qbSubjectArticle) {
        startPage();
        return getDataTable(qbSubjectArticleService.getArticleList(qbSubjectArticle));
    }

    /**
     * @param id
     * @description: 文章详情
     * @author: ChenYao
     * @return: com.xiaoye.qbproject.domain.AjaxResult
     * @ceateDate: 2025/1/15 下午8:11
     */

    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        QbSubjectArticle qbSubjectArticle = qbSubjectArticleService.getById(id);
        if (qbSubjectArticle == null) {
            return AjaxResult.error("文章不存在");
        }
        return AjaxResult.success(qbSubjectArticle);
    }

    // 新增文章
    @PostMapping
    public AjaxResult add(@RequestBody QbSubjectArticle qbSubjectArticle) {
        String subjectId = qbSubjectArticle.getSubjectId();
        if (StringUtils.isBlank(subjectId)) {
            return AjaxResult.error("请选择专栏");
        }
        return toAjax(qbSubjectArticleService.add(qbSubjectArticle));
    }

    // 修改文章
    @PutMapping
    public AjaxResult edit(@RequestBody QbSubjectArticle qbSubjectArticle) {
        String subjectId = qbSubjectArticle.getSubjectId();
        if (StringUtils.isBlank(subjectId)) {
            return AjaxResult.error("请选择专栏");
        }
        return toAjax(qbSubjectArticleService.editById(qbSubjectArticle));
    }

    /**
     * @param qbPublishVo
     * @description: 发布文章
     * @author: ChenYao
     * @return: com.xiaoye.qbproject.domain.AjaxResult
     */

    @PutMapping("/publish")
    public AjaxResult publish(@RequestBody QbPublishVo qbPublishVo) {
        return toAjax(qbSubjectArticleService.publish(qbPublishVo));
    }

    // 删除文章
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        if (ids == null || ids.length == 0) {
            return AjaxResult.error("请选择要删除的文章");
        }
        return toAjax(qbSubjectArticleService.removeByIds(Arrays.asList(ids)));
    }

    @GetMapping("/publishedCount/{userId}")
    public AjaxResult getPublishedCount(@PathVariable String userId) {
        if (StringUtils.isBlank(userId)) {
            return AjaxResult.error("用户ID不能为空");
        }
        return AjaxResult.success(qbSubjectArticleService.getPublishedCount(userId));
    }

    @GetMapping("/publishedArticles/{userId}")
    public AjaxResult getPublishedArticles(@PathVariable String userId,
                                           @RequestParam(required = false, defaultValue = "true")
                                           Boolean isFilterRead) {
        if (StringUtils.isBlank(userId)) {
            return AjaxResult.error("用户ID不能为空");
        }
        return AjaxResult.success(qbSubjectArticleService.getPublishedArticles(userId,isFilterRead));
    }

    @Autowired
    private MqReadService mqReadService;

    /**
     * @param readMsgVo
     * @description: 已读文章标记
     * @author: ChenYao
     * @return: com.xiaoye.qbproject.domain.AjaxResult
     * @ceateDate: 2025/4/26 下午11:05
     */

    @PutMapping("/readMsg")
    public AjaxResult readMsg(@RequestBody ReadMsgVo readMsgVo) {
        mqReadService.readMsg(readMsgVo);
        return AjaxResult.success();
    }

}
